package com.keafmd.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.keafmd.aspect.SelfZhuJie;
import com.keafmd.entity.EbSysArea;
import com.keafmd.entity.EbSysAreaVO;
import com.keafmd.entity.Test;
import com.keafmd.result.RespResult;
import com.keafmd.service.EbSysAreaService;
import com.keafmd.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private EbSysAreaService ebSysAreaService;

    @RequestMapping("/test6")
    public List<Test> testCDATA() {
        List<Test> list= testService.testCDATA();
        return list;
    }

    @RequestMapping("/excel")
    public void easyExcelTest() {
        QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
        List<Test> tests = testService.getBaseMapper().selectList(queryWrapper);
        File file = new File("C:\\Users\\chenji\\Desktop\\test.xlsx");
        //合并策略map
        Map<String, List<RowRangeDto>> strategyMap = ExcelUtil.addMerStrategy(tests);
        EasyExcelFactory.write(file, Test.class)
                //输出单元格合并策略
                        .registerWriteHandler(new BizMergeStrategy(strategyMap))
                .sheet().doWrite(tests);
    }

    @RequestMapping("/readexcel")
    public List<Test> readexcel() {
        File file = new File("C:\\Users\\chenji\\Desktop\\test.xlsx");
        //从第二行开始读
        return EasyExcelFactory.read(file).doReadAllSync();
    }

    @RequestMapping("/excel1")
    public void easyExcelTest1() {
        LambdaUpdateWrapper<Test> q = new LambdaUpdateWrapper<>();
        q.eq(Test::getId, 1).set(Test::getName1, null);
        testService.update(q);
    }

    @RequestMapping("/snowsuanfa")
    public void snowsuanfa() {
        //机房 id long类型  机器 id long类型
        Snowflake snowflake = IdUtil.getSnowflake(1L, 0L);
        Set<Long> longs = new HashSet<>();
        for (int i = 0; i < 1000000; i++) {
            long l = snowflake.nextId();
            longs.add(l);
        }
        System.out.println(longs.size() == 1000000);
        longs.forEach(System.out::println);
    }

    @RequestMapping("/area")
    public List<EbSysAreaVO> areaTree() {
        List<EbSysArea> list = ebSysAreaService.list();
        List<EbSysAreaVO> voList = BeanUtil.copyToList(list, EbSysAreaVO.class);
        List<EbSysAreaVO> collect1 = voList.stream().filter(e -> e.getAreaLevel() == 1).collect(Collectors.toList());
        Map<String, List<EbSysAreaVO>> collect2 = voList.stream()
                .filter(e -> e.getAreaLevel() == 2)
                .collect(Collectors.groupingBy(EbSysAreaVO::getParentId));
        Map<String, List<EbSysAreaVO>> collect3 = voList.stream()
                .filter(e -> e.getAreaLevel() == 3)
                .collect(Collectors.groupingBy(EbSysAreaVO::getParentId));
        for (EbSysAreaVO ebSysAreaVO : collect1) {
            ebSysAreaVO.setList(collect2.get(ebSysAreaVO.getAreaId()));
            for (EbSysAreaVO sysAreaVO : ebSysAreaVO.getList()) {
                sysAreaVO.setList(collect3.get(sysAreaVO.getAreaId()));
            }
        }
        return collect1;
    }

    @RequestMapping("/testreplace")
    public EbSysArea testreplace() {
        List<EbSysArea> list = ebSysAreaService.list();
        EbSysArea ebSysArea = list.get(0);
        String replace = ebSysArea.getAreaId().replace("100000", "1");
        log.error("异常",new RuntimeException("抛出异常"));
        log.info("replace:{}",replace);
        return ebSysArea;
    }

    @RequestMapping("/copyList")
    public List<EbSysAreaVO> copyList() {
        return ebSysAreaService.list().stream()
                .map(e ->BeanUtil.copyProperties(e, EbSysAreaVO.class))
                .collect(Collectors.toList());
    }

    @RequestMapping("/testerror")
    public List<EbSysAreaVO> testerror() {
        int a=1/0;
        return ebSysAreaService.list().stream()
                .map(e ->BeanUtil.copyProperties(e, EbSysAreaVO.class))
                .collect(Collectors.toList());
    }

    @RequestMapping("/testzhujie")
    @SelfZhuJie("ebSysArea")
    public EbSysArea testzhujie(@RequestBody EbSysArea ebSysArea) {
        System.out.println(ebSysArea);
        System.out.println(ebSysArea);
        return ebSysArea;
    }

    @RequestMapping("/testbeantomap")
    public Map<String,Object> testzhujie() {
                List<EbSysArea> list = ebSysAreaService.list();
               EbSysArea ebSysArea = list.get(Integer.parseInt("1"));
        Map<String, Object> map = BeanUtil.beanToMap(ebSysArea);
        return map;
    }


    @RequestMapping("/test1")
    public RespResult<String> test1() {
        Test test = new Test();
        test.setName1("插入的名字");
        testService.getBaseMapper().insert(test);
        System.out.println(test.getId());
        return RespResult.success("ok");
    }

    @RequestMapping("/test5")
    public RespResult<Test> test5() {
        Test test = new Test();
        test.setName1("测试");
        test.setId("3");
       Test t= testService.findByTest(test);
        return RespResult.success(t);
    }

    @RequestMapping("/test3")
    public RespResult<List> test3() {
        List<Test> list = testService.list(null);
        return RespResult.success(list);
    }

//    @RequestMapping("/test4")
//    public RespResult<String> test4() {
//        FutureTask<RespResult<String>> futureTask=new FutureTask<>(new CallableTest());
//        new Thread(futureTask).start();
//        RespResult<String> stringRespResult = null;
//        try {
//            stringRespResult = futureTask.get();
//            System.out.println(futureTask.isDone());
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println(futureTask.isCancelled());
//        return stringRespResult;
//    }

//    public class CallableTest implements Callable<RespResult<String>>{
//        @Override
//        public RespResult<String> call() throws Exception {
//            return RespResult.success("call方法返回的结果");
//        }
//    }
}
